package net.sesami.nhg.ebd.batch.dao;

import java.util.Calendar;
import java.util.List;

import net.sesami.nhg.ebd.batch.constants.IConstants;
import net.sesami.nhg.ebd.batch.domain.hibernate.PO;
import net.sesami.nhg.ebd.batch.domain.hibernate.POItem;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class PODAO {
	public PO findByPOID(Integer poid) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		PO po = (PO) session.load(PO.class, poid);
		session.close();

		return po;
	}

	public List<PO> getAllPOReadyToSend() {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.enableFetchProfile("PO-With-POItems");
		String hqlQuery = String
				.format("select po from %s po where po.POStatusID = :POStatusID and po.Transmit_Status_ID = :Transmit_Status_ID and po.Order_Status_ID = :Order_Status_ID",
						PO.class.getName());
		Query query = session.createQuery(hqlQuery);
		query.setInteger("POStatusID", IConstants.POStatus.APPROVED);
		query.setInteger("Transmit_Status_ID",
				IConstants.Transmit_Status.READY_TO_SEND);
		query.setInteger("Order_Status_ID", IConstants.Order_Status.NO_STATUS);
		List<PO> pos = query.list();
		for (PO po : pos) {
			Hibernate.initialize(po.getPoItems());
			Hibernate.initialize(po.getStatic_Payment());
			Hibernate.initialize(po.getStatic_Bill_To());
			Hibernate.initialize(po.getStatic_Ship_To());
			Hibernate.initialize(po.getStatic_Supplier());
			Hibernate.initialize(po.getStatic_Supplier().getSupplier());
			Hibernate.initialize(po.getStatic_Supplier().getShipMethod());
			Hibernate.initialize(po.getBorg());
			Hibernate.initialize(po.getBorg().getBillTo());
			Hibernate.initialize(po.getBorg().getBillTo().getAddress());
			Hibernate.initialize(po.getBorg().getBillTo().getContact());
			Hibernate.initialize(po.getPoAttachments());
			for (POItem poItem : po.getPoItems()) {
				Hibernate.initialize(poItem.getPoCostDistribs());
				Hibernate.initialize(poItem.getPoItemAttachments());
			}
		}
		session.close();

		return pos;
	}

	public void updatePOAsWaitingForSupplierResponse(Integer poId) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction t = session.beginTransaction();
		String hqlQuery = String
				.format("update %s set POStatusID = :POStatusID, Transmit_Status_ID = :Transmit_Status_ID, Order_Status_ID = :Order_Status_ID, POStatusDate = :POStatusDate where POID = :POID",
						PO.class.getName());
		Query query = session.createQuery(hqlQuery);
		query.setInteger("POStatusID", IConstants.POStatus.WAITING_FOR_RESPONSE);
		query.setInteger("Transmit_Status_ID",
				IConstants.Transmit_Status.SENT_TO_SUPPLIER);
		query.setInteger("Order_Status_ID", IConstants.Order_Status.NEW_ORDER);
		query.setDate("POStatusDate", Calendar.getInstance().getTime());
		query.setInteger("POID", poId);
		Integer i = query.executeUpdate();
		t.commit();
		session.close();
	}

	public void updateCanceledPOAsSentToSupplier(Integer poId) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction t = session.beginTransaction();
		String hqlQuery = String
				.format("update %s set Transmit_Status_ID = :Transmit_Status_ID, POStatusDate = :POStatusDate where POID = :POID",
						PO.class.getName());
		Query query = session.createQuery(hqlQuery);
		query.setInteger("Transmit_Status_ID",
				IConstants.Transmit_Status.SENT_TO_SUPPLIER);
		query.setDate("POStatusDate", Calendar.getInstance().getTime());
		query.setInteger("POID", poId);
		Integer i = query.executeUpdate();
		t.commit();
		session.close();
	}

	public List<PO> getAllCanceledPOReadyToSend() {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.enableFetchProfile("PO-With-POItems");
		String hqlQuery = String
				.format("select po from %s po where po.POStatusID = :POStatusID and po.Transmit_Status_ID = :Transmit_Status_ID and po.Order_Status_ID = :Order_Status_ID",
						PO.class.getName());
		Query query = session.createQuery(hqlQuery);
		query.setInteger("POStatusID", IConstants.POStatus.CANCELLED_BY_BUYER);
		query.setInteger("Transmit_Status_ID",
				IConstants.Transmit_Status.READY_TO_SEND);
		query.setInteger("Order_Status_ID", IConstants.Order_Status.CANCELLED);
		List<PO> pos = query.list();
		for (PO po : pos) {
			Hibernate.initialize(po.getPoItems());
			Hibernate.initialize(po.getStatic_Payment());
			Hibernate.initialize(po.getStatic_Bill_To());
			Hibernate.initialize(po.getStatic_Ship_To());
			Hibernate.initialize(po.getStatic_Supplier());
			Hibernate.initialize(po.getStatic_Supplier().getSupplier());
			Hibernate.initialize(po.getBorg());
			Hibernate.initialize(po.getBorg().getBillTo());
			Hibernate.initialize(po.getBorg().getBillTo().getAddress());
			Hibernate.initialize(po.getBorg().getBillTo().getContact());
			Hibernate.initialize(po.getPoAttachments());
			for (POItem poItem : po.getPoItems()) {
				Hibernate.initialize(poItem.getPoCostDistribs());
				Hibernate.initialize(poItem.getPoItemAttachments());
			}
		}
		session.close();

		return pos;
	}
}
